package org.lsms.servlet;

/**
 * servlet used to process the paper assignment(from chair to member)
 * @author ZhanXueliang
 * 2010.8.31
 */

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lsms.bean.User;
import org.lsms.dao.impl.DaoImpl;
import org.lsms.mail.MailSenderInfo;
import org.lsms.mail.SimpleMailSender;

public class paper_assign1 extends HttpServlet {

	public void destroy() {
		super.destroy(); 
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			int paper_id = Integer.parseInt(request.getParameter("paper_id"));
			int member_id = Integer.parseInt(request.getParameter("member_id"));
			//System.out.print(paper_id);
			//System.out.print(member_id);
			
			DaoImpl impl = new DaoImpl();
			
			User user = impl.get_user_info(member_id);
			String email = user.getEmail();
			String firstname = user.getFirstname();
			String middlename = user.getMiddlename();
			String lastname = user.getLastname();
			String loginname = user.getLoginname();
			
			MailSenderInfo mailInfo = new MailSenderInfo();
			mailInfo.init(mailInfo);
			mailInfo.setToAddress(email);
			mailInfo.setSubject("LSMS & ICSEE 2010 Paper Assign");
			
			String content = "Dear Mr/Mrs " + firstname+" "+middlename+" "+lastname+'\n'+'\n';
			String letter_content = impl.get_mail_content(3);
			content +=letter_content;
			String ps = "\n\nPS:\nYour Loginname:"+loginname+".\nif you have forgotten your password, " +
					"you can click the \"forget password?\" button to reset it.";
			content +=ps;
			mailInfo.setContent(content);
			
			
			SimpleMailSender sms = new SimpleMailSender();
			
			
			if(impl.paper_assign1(paper_id, member_id) && sms.sendTextMail(mailInfo)){
				String info = "The paper "+paper_id+" has been assigned to PC member " +
				member_id;;
				response.sendRedirect("chair/chair_show_info.jsp?chair_info="+info);
			}else{
				String info = "Problem occurs when assign papaer!Please try again later.";
				response.sendRedirect("chair/chair_show_info.jsp?chair_info="+info);
			}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
